package com.culture.config.controller;

import java.util.List;

import javax.validation.Valid;

import com.culture.config.common.model.ResponseData;
import com.culture.config.common.util.ResponseUtil;
import com.culture.config.service.UserService;
import com.culture.config.model.po.UserAddPO;
import com.culture.config.model.po.UserPageParamPO;
import com.culture.config.model.po.UserPasswordUpdatePO;
import com.culture.config.model.po.UserUpdatePO;
import com.culture.config.model.vo.UserVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping("/api/user")
public class UserController  extends BaseController{
    @Autowired
    private UserService userService;

    @ApiOperation("page user")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/page")
    public ResponseData<List<UserVO>> page(@RequestBody @Valid UserPageParamPO param){
        this.authenticationValidate();
        return ResponseUtil.successPage(this.userService.page(param));
    }   

    @ApiOperation("add user")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/add")
    public ResponseData<String> add(@RequestBody @Valid UserAddPO userAddPO){
        this.authenticationValidate();
        this.userService.add(userAddPO);
        return ResponseUtil.success();
    }

    @ApiOperation("update user")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/update")
    public ResponseData<String> update(@RequestBody @Valid UserUpdatePO userUpdatePO){
        this.authenticationValidate();
        this.userService.update(userUpdatePO);
        return ResponseUtil.success();
    }

    @ApiOperation("update user password")
	@PreAuthorize(value = "hasRole('ROLE_root')")
	@PostMapping("/update/password")
    public ResponseData<String> updatePassword(@RequestBody @Valid UserPasswordUpdatePO userPasswordUpdatePO){
        this.authenticationValidate();
        this.userService.updatePassword(userPasswordUpdatePO);
        return ResponseUtil.success();
    }
}
